package online.niuma.xiaocubao.config;

import online.niuma.xiaocubao.handler.LoginInterceptor;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author 一颗蛋50斤
 * IntelliJ IDEA
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private LoginInterceptor loginInterceptor;

    @Override
    public void addCorsMappings(@NotNull CorsRegistry registry) {
        // 跨域配置
        /* registry.addMapping("/**").allowedOrigins("http://127.0.0.1:3000/");*/
        registry.addMapping("/**")
                // .allowedOrigins("*")
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 配置放行路径
        ArrayList<String> urls = new ArrayList<>();
        urls.add("/favicon.ico");
        urls.add("/error");
        urls.add("/swagger-resources/**");
        urls.add("/webjars/**");
        urls.add("/v2/**");
        urls.add("/doc.html");
        urls.add("**/swagger-ui.html");
        urls.add("/swagger-ui.html/**");
        urls.add("/login");
        urls.add("/login/*");
        urls.add("/test/**");
        urls.add("/druid");
        urls.add("/upload/qiniu");


        registry.addInterceptor(loginInterceptor)
                // 拦截路径
                .addPathPatterns("/**")
                // 放行路径
                .excludePathPatterns(urls);
    }
}
